package com.weblog.article.controller;

import com.weblog.model.article.HotTag;
import com.weblog.model.article.Tag;
import com.weblog.article.service.TagService;
import entity.PageResult;
import entity.Result;
import entity.ResultMessage;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * tag控制器层
 * @author Administrator
 *
 */
@RestController
@CrossOrigin
@RequestMapping("/tag")
public class TagController {

	@Autowired
	private TagService tagService;



	/**
	 * 增加
	 * @param tag
	 */
	@PostMapping()
	public Result addTag(@RequestBody Tag tag ){
		int flag=tagService.addTag(tag);
		if (flag == 2) {
			return new Result(false, StatusCode.ACCESSERROR, ResultMessage.ACCESS_ERROR);
		}else if(flag==0){
			return new Result(false, StatusCode.REPERROR, ResultMessage.TAG_EXISTED);
		}
		return new Result(true, StatusCode.OK, ResultMessage.SAVE_SUCCESS);
	}


	/**
	 * 分页+多条件查询
	 * @param searchMap 查询条件封装
	 * @param page 页码
	 * @param size 页大小
	 * @return 分页结果
	 */
	@PostMapping(value="/search/{page}/{size}")
	public Result PageBySearch(@RequestBody Map<String,Object> searchMap , @PathVariable int page, @PathVariable int size){
		Page<Tag> pageList = tagService.PageBySearch(searchMap, page, size);
		return  new Result(true, StatusCode.OK,"查询成功",  new PageResult<Tag>(pageList.getTotalElements(), pageList.getContent()) );
	}


	/**
	 * 根据ID查询
	 * @param id ID
	 * @return
	 */
	@GetMapping(value="/{id}")
	public Result findById(@PathVariable String id){
		return new Result(true, StatusCode.OK, ResultMessage.QUERY_SUCCESS,tagService.findById(id));
	}



	/**
	 * 修改
	 * @param tag
	 */
	@PutMapping(value="/{id}")
	public Result update(@RequestBody Tag tag, @PathVariable String id ){
		tag.setId(id);
		int flag=tagService.update(tag);
		if(flag==2){
			return new Result(false, StatusCode.ACCESSERROR, ResultMessage.ACCESS_ERROR);
		}
		return new Result(true, StatusCode.OK, ResultMessage.UPDATE_SUCCESS);
	}



	/**
	 * 删除
	 * @param id
	 */
	@PutMapping(value="/delete/{id}")
	public Result delete(@PathVariable String id){
		int flag=tagService.deleteById(id);
		if(flag==2){
			return new Result(false, StatusCode.ACCESSERROR, ResultMessage.ACCESS_ERROR);
		}
		return new Result(true, StatusCode.OK, ResultMessage.DELETE_SUCCESS);
	}


	/**
	 * 查询前三个标签
	 * @param page
	 * @param size
	 * @return
	 */
	@PostMapping("/top3/{page}/{size}")
	public Result selectTop3(@PathVariable int page, @PathVariable int size){
		Page<Tag>tagPage=tagService.selectTop3(page,size);
		return new Result(true, StatusCode.OK, ResultMessage.QUERY_SUCCESS,new PageResult<Tag>(tagPage.getTotalElements(),tagPage.getContent()));
	}


	/**
	 * 查询前20个热门标签
	 * @return
	 */
	@PostMapping("/hotTag20")
	public Result hotTags20(){
		List<HotTag> hotTags = tagService.hotTags20();
		return new Result(true, StatusCode.OK, ResultMessage.QUERY_SUCCESS,hotTags);
	}




	/**
	 * 查询全部数据
	 * @return
	 */
	@RequestMapping(method= RequestMethod.GET)
	public Result findAll(){
		return new Result(true, StatusCode.OK,"查询成功",tagService.findAll());
	}
	




	/**
     * 根据条件查询
     * @param searchMap
     * @return
     */
    @RequestMapping(value="/search",method = RequestMethod.POST)
    public Result findSearch(@RequestBody Map searchMap){
        return new Result(true, StatusCode.OK,"查询成功",tagService.findSearch(searchMap));
    }
	



	
}
